exports.get = function(player, req, resp, onHandled) {
    resp.data.prison = player.user.prison;
    onHandled();
}

exports.get_target = function(player, req, resp, onHandled) {
    if( player.user.status.level < gConfGlobal.PrisonOpenLevelLimit ) {
        resp.code = 1; 
        resp.desc = 'level not reach'; 
        onHandled();
        return;
    }

    requestWorld(req, resp, onHandled);
}

exports.capture = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.pos) || isNaN(req.args.enemy) ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var pos = req.args.pos;
        var enemy = +req.args.enemy;
        var isCash = req.args.cash ? true:false;

        var user = player.user;
        if( user.status.level < gConfGlobal.PrisonOpenLevelLimit ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        if( !user.prison.prisons.hasOwnProperty(pos) ) {
            resp.code = 1; resp.desc = 'invalid pos'; break;
        }

        var costCash = 0;
        if( isCash ) {
            var confBuy = gConfBuy[user.prison.cash + 1];
            if( !confBuy ) {
                resp.code = 1; resp.desc = 'no more to buy'; break;
            }
                
            costCash = confBuy.CashPrison;
            var havecash = user.status.cash;
            if( havecash < costCash ) {
                resp.code = 1; resp.desc = 'not enough cash'; break;
            }
        }else{
            var prisonFreeLimit = gConfGlobal.PrisonDailyLimit;
            if( user.status.vip ) {
                prisonFreeLimit += gConfVip[user.status.vip].PrisonDailyLimit;
            }
            if( user.prison.free >= prisonFreeLimit ) {
                resp.code = 1; resp.desc = 'no more free'; break;
            }
        }

        requestWorld(req, resp, function() {
            if( resp.code == 0 ) {
                if( resp.data.battle.success ) {
                    if( isCash ) {
                        player.costResource('cash', costCash, user.prison.cash);
                        user.prison.cash += 1;
                        player.markDirty('prison.cash');
                    }else{
                        user.prison.free += 1;
                        player.markDirty('prison.free');
                    }

                    user.prison.prisons[pos] = resp.data.prison;
                    player.markDirty(util.format('prison.prisons.%d', pos));
                }else{
                    costCash = 0;
                }

                resp.data.cash = -costCash;

                player.doDailyTask(gConfDailyTask.Prison);
                resp.daily_task = gConfDailyTask.Prison;
            }
            onHandled();
        });
        return;
    }while(false);
    
    onHandled();
}

exports.resist = function(player, req, resp, onHandled) {
    var user = player.user;
    if( !user.prison.enemy ) {
        resp.code = 1; 
        resp.desc = 'no enemy'; 
        onHandled();
        return;
    }

    req.args.enemy = user.prison.enemy.uid;

    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            if( resp.data.battle.success ) {
                user.prison.enemy = 0;
                player.markDirty('prison.enemy');
            }
        }
        onHandled();
    });
}

exports.press = function(player, req, resp, onHandled) {
    var uid = req.uid;
    do{
        if( isNaN(req.args.pos) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }
        
        var pos = +req.args.pos;
        var user = player.user;

        var prison = user.prison.prisons[pos];
        if( !prison ) {
            resp.code = 1; resp.desc = 'empty prison'; break;
        }
        
        var costCash = 0;
        var now = common.getTime();
        if( prison.timer > now ) {
            costCash = Math.ceil((prison.timer - now)/3600 * gConfGlobal.PrisonPressCash); 
        }
        
        if( !player.costResource('cash', costCash) ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }

        var addGold = prison.output;
        user.status.gold += addGold;
        player.markDirty('status.gold');

        user.prison.prisons[pos] = 0;
        player.markDirty(util.format('prison.prisons.%d',pos));

        var awards = [];
        if (prison.award) {
            var awardConf = gConfPrisonAward[prison.award];
            awards.push(awardConf.Award1);
        }
        
        resp.data.awards = player.addAwards(awards);
        resp.data.cash = -costCash;
        resp.data.gold = addGold;

    }while(false);

    onHandled();
}

exports.auto_free = function(player, req, resp, onHandled) {
    var uid = req.uid;
    var user = player.user;

    var now = common.getTime();
    
    var freedPoss = [];
    var addGold = 0;
    var awards = [];

    var prisons = user.prison.prisons;
    for( var pos in prisons ) {
        var prison = prisons[pos];
        if( !prison || (now+10) < prison.timer) { // 误差修正
            continue;
        }

        if (prison.award) {
            var awardConf = gConfPrisonAward[prison.award];
            awards.push(awardConf.Award1);
        }
        
        addGold += prison.output;
        freedPoss.push(pos);
        prisons[pos] = 0;
    }

    if( freedPoss.length > 0 ) {
        player.markDirty('prison.prisons');
        user.status.gold += addGold;
        player.markDirty('status.gold');
    }
    
    resp.data.awards = player.addAwards(awards);
    resp.data.gold = addGold;
    resp.data.freed = freedPoss;
    onHandled();
}
